package com.zmn.plat.admin.controller.push;

import com.google.common.collect.Lists;
import com.zmn.common.dto.VtDTO;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.logger.LoggerUtil;
import com.zmn.common.utils.office.ExcelUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.plat.common.exception.PlatException;
import com.zmn.plat.model.vo.push.PushNoticeAppVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 描述: app公告逻辑控制器
 *
 * @author wangjie
 * @since 2020/3/23
 **/
@Slf4j
@RestController
@RequestMapping(value = "/push/noticeApp/")
public class PushNoticeModifyController extends PushNoticeAppBaseController {

    /**
     * 描述: 新增和修改
     *
     * @param pushNoticeAppVo 消息推送对象
     * @return com.zmn.common.dto2.ResponseDTO<java.lang.String>
     * @author wangjie
     * @since 2020/3/23
     **/
    @RequestMapping(value = "saveAndEdit")
    public ResponseDTO<String> saveAndEdit(@RequestBody PushNoticeAppVo pushNoticeAppVo) {

        try {
            pushNoticeAppBussService.saveAndEdit(pushNoticeAppVo, getMcStaff().getRealName());
            return ResponseDTO.success("操作成功！");
        } catch (Exception e) {
            log.error("[{}],新增app公告数据失败，msg:{}", TAG, e.getMessage(), e);
            return ResponseDTO.fail("操作失败，请联系管理员！");
        }
    }

    /**
     * 描述: 修改排序号
     *
     * @param id   id
     * @param sort 排序号
     * @return com.zmn.common.dto2.ResponseDTO<java.lang.String>
     * @author wangjie
     * @since 2020/3/23
     **/
    @RequestMapping(value = "setSort")
    public ResponseDTO<String> setSort(@RequestParam(value = "id") Integer id, @RequestParam(value = "sort") Integer sort) {

        try {
            pushNoticeAppService.updateSortByKey(id, sort, getMcStaff().getRealName());
            return ResponseDTO.success("操作成功", "操作成功");
        } catch (Exception e) {
            log.error("[{}],程序错误,操作失败,msg:{}", TAG, e.getMessage(), e);
            return ResponseDTO.fail("程序错误,操作失败");
        }
    }

    /**
     * 描述: 修改发布状态
     *
     * @param id     id
     * @param status 发布状态
     * @return com.zmn.common.dto2.ResponseDTO<java.lang.String>
     * @author wangjie
     * @since 2020/3/23
     **/
    @RequestMapping(value = "setStatus")
    public ResponseDTO<String> setStatus(@RequestParam(value = "id") Integer id, @RequestParam(value = "status") Integer status) {

        try {
            pushNoticeAppService.updateStatusByKeys(Collections.singletonList(id), status, getMcStaff().getRealName());
            pushNoticeAppBussService.submitPushMsgTask(id, getMcStaff().getRealName());
            return ResponseDTO.success("操作成功", "操作成功");
        } catch (Exception e) {
            log.error("[{}],程序错误,操作失败,msg:{}", TAG, e.getMessage(), e);
            return ResponseDTO.fail("程序错误,操作失败");
        }
    }

    /**
     * 描述: 修改发布状态
     *
     * @param ids    id集合
     * @param status 发布状态
     * @return com.zmn.common.dto2.ResponseDTO<java.lang.String>
     * @author wangjie
     * @since 2020/3/30
     **/
    @RequestMapping(value = "setStatusByList")
    public ResponseDTO<String> setStatusByList(@RequestParam(value = "ids") String ids
            , @RequestParam(value = "status") Integer status) {
        try {
            List<Integer> split = StringUtil.split(ids, Integer.class);
            pushNoticeAppService.updateStatusByKeys(split, status, getMcStaff().getRealName());
            split.forEach(item -> pushNoticeAppBussService.submitPushMsgTask(item, getMcStaff().getRealName()));
            return ResponseDTO.success("操作成功", "操作成功");
        } catch (Exception e) {
            log.error("[{}],程序错误,操作失败,msg:{}", TAG, e.getMessage(), e);
            return ResponseDTO.fail("程序错误,操作失败");
        }
    }

    /**
     * 描述: 加载二级区县id
     *
     * @param pid 父级ID
     * @return com.zmn.common.dto2.ResponseDTO<java.util.List < com.zmn.common.dto.VtDTO>>
     * @author wangjie
     * @since 2020/3/24
     **/
    @RequestMapping(value = "loadTwoDeptSelect")
    public ResponseDTO<List<VtDTO>> loadTwoDeptSelect(Integer pid) {
        try {
            return ResponseDTO.success(pushNoticeAppBussService.listCacheDeptByPid(pid), "加载成功");
        } catch (Exception e) {
            log.error("[{}],程序错误,操作失败,msg:{}", TAG, e.getMessage(), e);
            return ResponseDTO.fail("程序错误,操作失败");
        }
    }

    /**
     * 描述: 加载城市数据
     *
     * @param pid 父级ID
     * @return com.zmn.common.dto2.ResponseDTO<java.util.List < com.zmn.common.dto.VtDTO>>
     * @author wangjie
     * @since 2020/3/24
     **/
    @RequestMapping(value = "loadCitySelect")
    public ResponseDTO<List<VtDTO>> loadCitySelect(String pid) {

        try {
            List<Integer> pidList = StringUtil.split(pid, ",", Integer.class);
            if (CollectionUtil.isNullOrEmpty(pidList)) {
                return ResponseDTO.success(pushNoticeAppBussService.listAllCacheCity(), "加载成功");
            }
            return ResponseDTO.success(pushNoticeAppBussService.listCacheCityByProvinceId(pidList), "加载成功");
        } catch (Exception e) {
            log.error("[{}],程序错误,操作失败,msg:{}", TAG, e.getMessage(), e);
            return ResponseDTO.fail("程序错误,操作失败");
        }
    }

    /**
     * 描述: 加载城市数据
     *
     * @param pid 父级ID
     * @return com.zmn.common.dto2.ResponseDTO<java.util.List < com.zmn.common.dto.VtDTO>>
     * @author wangjie
     * @since 2020/3/24
     **/
    @RequestMapping(value = "loadCountySelect")
    public ResponseDTO<List<VtDTO>> loadCountySelect(String pid) {

        try {
            List<Integer> pidList = StringUtil.split(pid, ",", Integer.class);
            if (CollectionUtil.isNullOrEmpty(pidList)) {
                return ResponseDTO.success(pushNoticeAppBussService.listAllCacheCounty(), "加载成功");
            }
            return ResponseDTO.success(pushNoticeAppBussService.listCacheCountyByCityId(pidList), "加载成功");
        } catch (Exception e) {
            log.error("[{}],程序错误,操作失败,msg:{}", TAG, e.getMessage(), e);
            return ResponseDTO.fail("程序错误,操作失败");
        }
    }

    /**
     * 描述: 根据平台标识公司类型查询子公司数据
     *
     * @param types 公司类型
     * @param plats 平台标识
     * @return com.zmn.common.dto2.ResponseDTO<java.util.List < com.zmn.common.dto.VtDTO>>
     * @author wangjie
     * @since 2020/3/25
     **/
    @RequestMapping(value = "loadCompany")
    public ResponseDTO<List<VtDTO>> loadCompany(@RequestParam(value = "types") String types, @RequestParam(value = "plats") String plats) {

        try {
            List<Integer> platList = StringUtil.split(plats, ",", Integer.class);
            if (CollectionUtil.isNullOrEmpty(platList)) {
                return ResponseDTO.fail("请选择平台");
            }
            List<Integer> typeList = StringUtil.split(types, ",", Integer.class);
            if (CollectionUtil.isNullOrEmpty(typeList)) {
                return ResponseDTO.fail("请选择公司类型");
            }
            List<VtDTO> companyVt = pushNoticeAppBussService.listCompanyByTypeAndPlat(typeList, platList);
            return ResponseDTO.success(companyVt, "操作成功");
        } catch (Exception e) {
            log.error("[{}],程序错误,操作失败,msg:{}", TAG, e.getMessage(), e);
            return ResponseDTO.fail("程序错误,操作失败");
        }
    }

    /**
     * 描述: 下载工程app公告通知模板
     *
     * @author wangjie
     * @since 2020/3/30
     **/
    @RequestMapping(value = "outPutTemplate")
    public void outPutTemplate(HttpServletResponse response) {

        List<String> title = Arrays.asList("平台", "工程师ID", "工程师姓名");
        ArrayList<List> lists = Lists.newArrayList();
        lists.add(title);
        try {
            String name = "投放工程师模板.xlsx";
            name = new String(name.getBytes(), "ISO8859-1");
            ExcelUtil.export(response, lists, name, "sheet");
        } catch (Exception e) {
            LoggerUtil.error(this.getClass(), "导出报表出错" + e.getMessage(), e);
        }
    }

    /**
     * 描述: 导入excel
     *
     * @param file 传入的文件对象
     * @return com.zmn.common.dto2.ResponseDTO<java.lang.String>
     * @author wangjie
     * @since 2020/3/30
     **/
    @RequestMapping(value = "importExcel")
    public ResponseDTO<String> importExcel(@RequestParam(value = "file") MultipartFile file,
                                           @RequestParam(value = "noticeId") Integer noticeId) {

        try {
            String result = pushNoticeAppBussService.importExcel(file, noticeId, getMcStaff().getRealName());
            return ResponseDTO.success(result);
        } catch (PlatException e) {
            log.error("importExcel error.", e);
            return ResponseDTO.fail(e.getMessage());
        } catch (Exception e) {
            log.error("importExcel error.", e);
            return ResponseDTO.fail("");
        }

    }

}
